﻿
//监听好友状态
function FriendsManage() {
    if (document.getElementById("myFriendsList").innerHTML== "加载中…") {
        document.getElementById("myFriendsList").innerHTML ="";
    }
    if (this.req.responseText == "")
    { return; }
    else {
        var friendsList = DecodeFriendsListXml(this.req.responseText);
        for (var i = 0; friendsList[i] != undefined; i++) {
            if (GetFriendItem(friendsList[i][0]) == null) {
                var Item = document.createElement("<div class='friendItem' ondblclick='NewChatWindow(this);'  ></div>");
                Item.appendChild(document.createElement("<img onmouseover='friendDetailFloatWindow(this);'src=" + friendsList[i][3] + " />"));
                var name = document.createElement("<label  class='friendName' value='" + friendsList[i][0] + "' /></label>");
                name.innerHTML = friendsList[i][0];
                Item.appendChild(name);
                document.getElementById("myFriendsList").appendChild(Item);
            }
            else {
                if (friendsList[i][2] == "OnLine") {
                    if (GetFriendItem(friendsList[i][0]).childNodes[1].style.fontWeight == "bold" ) continue;
                    InsertToFirst(friendsList[i][0]);
                    GetFriendItem(friendsList[i][0]).childNodes[1].style.fontWeight = "bold";
                }
                else if (friendsList[i][2] == "OffLine") {
                if (GetFriendItem(friendsList[i][0]).childNodes[1].style.fontWeight == "bold") {
                    InsertToLast(friendsList[i][0]);
                    GetFriendItem(friendsList[i][0]).childNodes[1].style.fontWeight = "normal";
                   }
                }
            }
        } 
     }
    
 }

 function GetFriendItem(name) {
     for (var i = 0; document.getElementById("myFriendsList").childNodes[i]!= undefined; i++) {
         if (document.getElementById("myFriendsList").childNodes[i].childNodes[1].value == name) return document.getElementById("myFriendsList").childNodes[i];
     } 
     return null;
 }


 function NewMSGForetell(name) {
     GetFriendItem(name).style.backgroundColor = "#99cccc";
 }
 function MSGForetellEnd(name) {
     GetFriendItem(name).style.backgroundColor = "white";
 }

 function InsertToFirst(name) {
     var temp = GetFriendItem(name);
     if (document.getElementById("myFriendsList").childNodes[0] == temp) return;
     document.getElementById("myFriendsList").insertBefore(temp,document.getElementById("myFriendsList").childNodes[0]);
 }
 function InsertToLast(name) {
     var temp = GetFriendItem(name);
     if (document.getElementById("myFriendsList").childNodes[0] == temp) return;
     document.getElementById("myFriendsList").appendChild(temp);
 }
 
 

 function ListenFriends() {
     var url = "AjaxResponse/GetFriendsList.aspx?name=" + MY_NAME;
    new ContentLoader(url, FriendsManage);
}

function DecodeFriendsListXml(XmlString) { //返回好友数组
    var xmlDoc = loadXML(XmlString);
    var FriendsList = new Array();
    for (var i = 0; xmlDoc.documentElement.childNodes[i] != null; i++) {
        FriendsList[i] = new Array();
        for (var j = 0; xmlDoc.documentElement.childNodes[i].childNodes[j] != null; j++) {
            FriendsList[i][j] = xmlDoc.documentElement.childNodes[i].childNodes[j].childNodes[0].nodeValue;
        }
    }
    return FriendsList;
    }

    function friendDetailFloatWindow(source) {
        var name = source.parentNode.childNodes[1].value;
        
        var nfw = NewFloatWindow(source, source.parentNode, "Right",10);
        nfw.innerHTML = "<label style='cursor:pointer;' class='friendName' onclick ='FriendProfileReq(this);' name='" + name + "' />好友资料查看</label><br/>" +
                "<label style='cursor:pointer;' class='friendName' onclick ='GetMSGRecord(this);' name='" + name + "' />聊天记录查看</label>";
        nfw.style.border = "solid 1px black";
        nfw.style.display = "none";

        var WindowDisplay = function() { if ( nfw != null&& nfw.parentNode != undefined) nfw.style.display = "block" }
        var WindowDelete = function() { if (nfw != null&& nfw.parentNode != undefined ) nfw.parentNode.removeChild(nfw); nfw = null; }
        
        var DeleteTimeout = setTimeout(WindowDelete, 4000);
        var DisplayTimeout = setTimeout(WindowDisplay, 1000);

        var bug = 0;//发觉浮动窗口有个显示的问题，有时候在mouse over进浮动窗口老是调用mouseout事件，妈的，找不到原因，所以叫他bug
        nfw.onmouseout = function() { clearTimeout(DeleteTimeout); window.status = bug; if (bug == 1) return; clearTimeout(DisplayTimeout); if (nfw.parentNode != undefined) WindowDelete(); }
        source.onmouseout = function() {clearTimeout(DeleteTimeout); DeleteTimeout = setTimeout(WindowDelete, 500); }
        nfw.onmouseover = function() { clearTimeout(DeleteTimeout);  window.status = bug; bug++;}
    }
    
    